package com.qf.util;

import com.qf.constant.RabbitMQConstants;
import com.qf.constant.ReportStateConstants;
import com.qf.constant.SourceType;
import com.qf.model.StandardReport;
import com.qf.model.StandardSubmit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PushMsgUtil {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 将日志信息发送给MQ
     * @param submit
     * @param errorCode
     */
    public void sendLog(StandardSubmit submit, String errorCode) {
        // 将errorCode封装的到submit
        submit.setErrorCode(errorCode);
        // 只要在策略模块发送了写日志的消息,将submit中的state设置为2
        submit.setReportState(ReportStateConstants.FAIL);
        // 将submit作为消息发送到SendLog的交换机中
        rabbitTemplate.convertAndSend(RabbitMQConstants.TOPIC_SMS_SEND_LOG + "_exchange","routingKey",submit);
    }

    /**
     * 将状态报告发送给MQ
     * @param submit
     * @param errorCode
     */
    public void pushReport(StandardSubmit submit, String errorCode) {
        // 如果发送短信的方式是HTTP，再给客户推送状态报告
        if(submit.getSource() == SourceType.HTTP) {
            // 封装standardReport对象
            StandardReport report = new StandardReport();
            report.setClientID(submit.getClientID());
            report.setState(ReportStateConstants.FAIL);
            report.setSrcID(submit.getSrcSequenceId());
            report.setErrorCode(errorCode);
            report.setMobile(submit.getDestMobile());
            report.setSequenceId(submit.getSequenceId());    // 涉及到搜索模块时,再添加

            // 将report作为消息发送到PushSmsReport队列
            rabbitTemplate.convertAndSend(RabbitMQConstants.TOPIC_PUSH_SMS_REPORT + "_exchange", "routingKey", report);
        }
    }
}
